﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
namespace PrinteMaster
{
    class Function
    {
        public static void registerSysmtemSetting(string ip,int port)
        {
            try
            {
                Registry.SetValue(keyname, "ip", ip);
                Registry.SetValue(keyname, "port", port);
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }
        private static string keyname = "HKEY_CURRENT_USER\\Software\\Leopard\\PrinterMasterClient";
        
        public static void getSystemSetting(out string ip, out int port)
        {
            ip = null; port = 0;
            try
            {
                ip = Registry.GetValue(keyname, "ip", null).ToString();
                port = (int)Registry.GetValue(keyname, "port", 0);
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }
        /// <summary>
        /// 该函数用于实现获得焦点时显示空值，不获得焦点时显示提示的功能。(普通输入框)
        /// </summary>
        /// <param name="tb">需要实现此功能的TextBox</param>
        /// <param name="hits">此功能实现时需要的提示</param>
        public static void inputText(TextBox tb,string hits)
        {
            if (tb.Text.Equals(hits))
            {
                tb.Text = "";
            }
            // 如果为空，则变为提示信息
            else if (tb.Text.Equals(""))
            {
                tb.Text = hits;
            }
        }

        /// <summary>
        /// 用于实现只能输入整数的功能.
        /// </summary>
        /// <param name="sender">原样传递</param>
        /// <param name="e">原样传递</param>
        public static void inputInt(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsNumber(e.KeyChar) && e.KeyChar != (char)8)
            {
                e.Handled = true;
            }
        }
        /// <summary>
        /// 该函数用于实现获得焦点时显示空值，不获得焦点时显示提示的功能。(密码输入框)
        /// </summary>
        /// <param name="tb"></param>
        /// <param name="hits"></param>
        public static void inputPassWord(TextBox tb, string hits)
        {
            // 如果账号为提示信息，则变为空
            if (tb.Text.Equals(hits))
            {
                tb.Text = "";
                tb.UseSystemPasswordChar = true;
            }
            // 如果为空，则变为提示信息
            else if (tb.Text.Equals(""))
            {
                tb.Text = hits;
                tb.UseSystemPasswordChar = false;
            }
        }

        /// <summary>
        /// 判断当前密码的合法性
        /// </summary>
        /// <param name="tb"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool isPassWordValue(TextBox tb,int value=8)
        {
            if (tb.Text.Length < value)
            {
                return false;
            }
            return true;
        }
        /// <summary>
        /// 判断当前字体长度的合法性
        /// </summary>
        /// <param name="str"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool isPassWordValue(string str, int value = 8)
        {
            if (str.Length < value)
            {
                return false;
            }
            return true;
        }

        #region 导出到excel

        public static void ExportExcel(string fileName, DataGridView myDGV)  
       {  
           if (myDGV.Rows.Count > 0)  
           {  
  
               string saveFileName = "";  
               //bool fileSaved = false;  
               SaveFileDialog saveDialog = new SaveFileDialog();  
               saveDialog.DefaultExt = "xls";  
               saveDialog.Filter = "Excel文件|*.xls";  
               saveDialog.FileName = fileName;  
               saveDialog.ShowDialog();  
               saveFileName = saveDialog.FileName;  
               if (saveFileName.IndexOf(":") < 0) return; //被点了取消   
               Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();  
               if (xlApp == null)  
               {  
                   MessageBox.Show("无法创建Excel对象，可能您的机子未安装Excel");  
                   return;  
               }  
  
               Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;  
               Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);  
               Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];//取得sheet1  
  
               //写入标题  
               for (int i = 0; i < myDGV.ColumnCount; i++)  
               {  
                   worksheet.Cells[1, i + 1] = myDGV.Columns[i].HeaderText;  
               }  
               //写入数值  
               for (int r = 0; r < myDGV.Rows.Count; r++)  
               {  
                   for (int i = 0; i < myDGV.ColumnCount; i++)  
                   {  
                       worksheet.Cells[r + 2, i + 1] = myDGV.Rows[r].Cells[i].Value;  
                   }  
                   System.Windows.Forms.Application.DoEvents();  
               }  
               worksheet.Columns.EntireColumn.AutoFit();//列宽自适应  
               //if (Microsoft.Office.Interop.cmbxType.Text != "Notification")  
               //{  
               //    Excel.Range rg = worksheet.get_Range(worksheet.Cells[2, 2], worksheet.Cells[ds.Tables[0].Rows.Count + 1, 2]);  
               //    rg.NumberFormat = "00000000";  
               //}  
  
               if (saveFileName != "")  
               {  
                   try  
                   {  
                       workbook.Saved = true;  
                       workbook.SaveCopyAs(saveFileName);  
                       //fileSaved = true;  
                   }  
                   catch (Exception ex)  
                   {  
                       //fileSaved = false;  
                       MessageBox.Show("导出文件时出错,文件可能正被打开！\n" + ex.Message);  
                   }  
  
               }  
               //else  
               //{  
               //    fileSaved = false;  
               //}  
               xlApp.Quit();  
               GC.Collect();//强行销毁   
               // if (fileSaved && System.IO.File.Exists(saveFileName)) System.Diagnostics.Process.Start(saveFileName); //打开EXCEL  
               MessageBox.Show(fileName + "的简明资料保存成功", "提示", MessageBoxButtons.OK);  
           }  
           else  
           {  
               MessageBox.Show("报表为空,无表格需要导出","提示",MessageBoxButtons.OK);  
           }  
  
       }  
        
       #endregion  
    }
}
